<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
const userStore = useUserStore()
const router = useRouter()
defineOptions({ name:"indexItem" })

const form = ref(null)
const ruleForm = ref({
    account:'',
    password:'',
})
// 登录校验规则
const rules = ref({
    // 用户名规则
    account: [
        { required: true, trigger: 'blur', message: '请输入用户名'}
    ],
    // 密码规则
    password:[
        { required: true, trigger: 'blur', message: '请输入密码'}
    ]
})
const checked2 = ref(false)
const submitForm = async() => {
    await form.value.validate()
    if( !checked2.value ) return ElMessage.error('请勾选用户协议')
    await userStore.getUserInfo(ruleForm.value)
    router.push('/home')
    ElMessage.success('登录成功')
    userStore.setToken(userStore.userInfo.token)
}
const resetForm = () => {
    ruleForm.value.account = '';
    ruleForm.value.password = "";
    checked2.value = false
}
</script>
<template>
 <div class="img"><img src="@/assets/images/login-bg.png" alt=""></div>
 <div class="info">
    <h1 style="text-align: center; text-shadow: 2px 2px 5px #999; margin-bottom: 20px;">用户登录</h1>
   <el-form  class="demo-ruleForm"  :model="ruleForm" :rules="rules" ref="form">
    <!-- 账号框 -->
    <el-form-item label="账户：" prop="account" class="login">
      <el-input autocomplete="off"  v-model="ruleForm.account" placeholder="请输入用户名"/>
    </el-form-item>
    <!-- 密码框 -->
    <el-form-item label="密码：" prop="password">
       <el-input  type="password" autocomplete="off" v-model="ruleForm.password" placeholder="请输入密码"/>
    </el-form-item>
    <!-- 我已同意用户协议 -->
    <div class="checkbox" style="text-align: center; margin-bottom: 10px;">
        <el-checkbox label="我已经同意用户协议" v-model="checked2"/>
    </div>
    <!-- 提交按钮和重制按钮 -->
     <el-form-item class="btn" label-width="65">
        <el-button type="primary" @click="submitForm" style="width: 110px;">登录</el-button>
        <el-button @click="resetForm" style="width: 110px;" >重制</el-button>
    </el-form-item>
   </el-form>
</div>
</template>
<style scoped>
.img{
 margin-top: 100px;
}
  .info{
    position: absolute;
    overflow: hidden;
    right: 100px;
    top: 150px;
    width: 500px;
    height: 300px;
    background-color: #fff;
    padding: 20px 100px 0;
    border-radius: 10px;
    opacity: .9;
  }
 .demo-ruleForm{
    margin: 0 auto;
 }
</style>